package com.infore.dataReceiving.select.filter.condition;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang.StringUtils;
import com.infore.dataReceiving.select.chain.ConditionFilterChain;
import com.infore.dataReceiving.select.enums.OperatorEnum;
import com.infore.dataReceiving.select.filter.ConditionFilter;
import com.infore.dataReceiving.select.util.QueryUtil;

/**
 * 不匹配过滤器
 */
public class NotInFilter implements ConditionFilter {
	
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void doFilter(List<Predicate> predicates, CriteriaBuilder criteriaBuilder, Root<?> root, String property,
			String operator, String content, ConditionFilterChain chain,boolean interrupt) {
		if(OperatorEnum.NOT_IN.getOperator().equals(operator)) {
			Path path = QueryUtil.getPropertyPath(property, root);
			if (StringUtils.isNotBlank(content)) {
				List<String> collection = Arrays.asList(content.split(","));
				Iterator iterator = collection.iterator();
				In<Object> in = criteriaBuilder.in(path);
				while (iterator.hasNext()) {
					in.value(iterator.next());
				}
				predicates.add(criteriaBuilder.not(in));
			}
			interrupt = true;
		}
		chain.doFilter(predicates,criteriaBuilder,root,property,operator,content,chain,interrupt);
		
	}
}
